package com.example.camera2sample

import android.content.pm.PackageManager
import android.graphics.SurfaceTexture
import android.os.Bundle
import android.util.Log
import android.view.TextureView
import android.widget.Button
import androidx.annotation.MainThread
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {

    companion object {
        private const val REQUEST_PERMISSION_CODE: Int = 1
        private val REQUIRED_PERMISSIONS: Array<String> = arrayOf(
                android.Manifest.permission.CAMERA,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
    }

    private val camera2Helper: ICamera by lazy { Camera2Helper(this) }
    private lateinit var textureView: TextureView
    private lateinit var previewSurfaceTextureListener: PreviewSurfaceTextureListener

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textureView = findViewById(R.id.textureView)
        previewSurfaceTextureListener = PreviewSurfaceTextureListener()

        findViewById<Button>(R.id.change_camera_button).setOnClickListener {
            camera2Helper.changeCamera()
        }

    }

    private inner class PreviewSurfaceTextureListener : TextureView.SurfaceTextureListener {
        @MainThread
        override fun onSurfaceTextureSizeChanged(surfaceTexture: SurfaceTexture, width: Int,
                                                 height: Int) = Unit

        @MainThread
        override fun onSurfaceTextureUpdated(surfaceTexture: SurfaceTexture) = Unit

        @MainThread
        override fun onSurfaceTextureDestroyed(surfaceTexture: SurfaceTexture): Boolean = false

        @MainThread
        override fun onSurfaceTextureAvailable(surfaceTexture: SurfaceTexture, width: Int,
                                               height: Int) {
            camera2Helper.setSurfaceTexture(surfaceTexture)
            camera2Helper.openCamera(width, height)
            Log.e("jiang", "onSurfaceTextureAvailable: " + width + "x" + height)
        }
    }

    override fun onResume() {
        super.onResume()
        if (checkRequiredPermission()) {
            camera2Helper.onResume()
            if (textureView.isAvailable) {
                camera2Helper.openCamera(textureView.width, textureView.height)
            } else {
                textureView.surfaceTextureListener = previewSurfaceTextureListener
            }
        }
    }

    override fun onPause() {
        camera2Helper.onPause()
        super.onPause()
    }


    /**
     * 权限申请
     */
    private fun checkRequiredPermission(): Boolean {
        val deniedPermissions = mutableListOf<String>()
        for (requiredPermission in REQUIRED_PERMISSIONS) {
            if (ContextCompat.checkSelfPermission(this, requiredPermission) == PackageManager.PERMISSION_DENIED) {
                deniedPermissions.add(requiredPermission)
            }
        }

        if (deniedPermissions.isEmpty().not()) {
            requestPermissions(deniedPermissions.toTypedArray(), REQUEST_PERMISSION_CODE)
        }
        return deniedPermissions.isEmpty()
    }


}